﻿using EnitityFrameworkSample.Data.Models;
using EntityFrameworkSample.Data;
using EntityFrameworkSample.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EntityFrameworkSample.Adapters.Data
{
    public class HomeDataAdapter : IHomeAdapter
    {
        public void JustDoSomething()
        {

        }
        public EnrollmentViewModel GetEnrollmentViewModel()
        {
            EnrollmentViewModel model = new EnrollmentViewModel();

            using (SchoolDbContext db = new SchoolDbContext())
            {
                model.Enrollments = db.SemesterStudentCourses
                    .Select(ssc => new EnrollmentItemViewModel
                    {
                        StudentId = ssc.StudentId,
                        CourseName = ssc.Course.Name,
                        FirstName = ssc.Student.FirstName,
                        LastName = ssc.Student.LastName,
                        SemesterEndDate = ssc.Semester.EndDate,
                        SemesterStartDate = ssc.Semester.StartDate
                    }).ToList();
            }

            return model;
        }


        public void CreateStudent(StudentViewModel model)
        {
            using (SchoolDbContext db = new SchoolDbContext())
            {
                Student dbStudent = new Student();

                dbStudent.FirstName = model.FirstName;
                dbStudent.LastName = model.LastName;

                //Create the INSERT SQL to add the student to the database
                db.Students.Add(dbStudent);

                //Create and execute the SELECT SQL statement to get the first course and semester
                //from the database
                Course dbCourse = db.Courses.FirstOrDefault();
                Semester dbSemester = db.Semesters.FirstOrDefault();

                SemesterStudentCourse dbSemStuCourse = new SemesterStudentCourse();
                dbSemStuCourse.Course = dbCourse;
                dbSemStuCourse.Student = dbStudent;
                dbSemStuCourse.Semester = dbSemester;

                //Create the INSERT SQL to add the SemesterStudentCourse to the database
                db.SemesterStudentCourses.Add(dbSemStuCourse);

                db.SaveChanges();

                //string sqlString = "INSERT INTO Students (FirstName, LastName) VALUES ('bob', 'tom')";
            }
        }

        public void UpdateStudent(StudentViewModel model)
        {
            using (SchoolDbContext db = new SchoolDbContext())
            {
                Student dbStudent = db.Students.Find(model.StudentId);
                dbStudent.FirstName = model.FirstName;
                dbStudent.LastName = model.LastName;

                db.SaveChanges();
            }
        }

        public StudentViewModel GetStudentViewModel(int id)
        {
            StudentViewModel model = new StudentViewModel();

            using (SchoolDbContext db = new SchoolDbContext())
            {
                Student dbStudent = db.Students.Find(id);
                model.FirstName = dbStudent.FirstName;
                model.LastName = dbStudent.LastName;
                model.StudentId = dbStudent.StudentId;
            }

            return model;
        }
    }
}